﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace AnesSystem.ViewModels
{
    public class DrugListCtrlViewModel : BaseViewModel<DrugListCtrlViewModel>
    {

        #region Variables

        private ObservableCollection<DrugListItemCtrl2> _displayOprList = new ObservableCollection<DrugListItemCtrl2>();
        private DateTime _selectDateTime = DateTime.Now;
        private DateTime _startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        private DateTime _endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        private int itemCount = 29;
        private Visibility _dateVis;

        #endregion

        #region Constructor
        public DrugListCtrlViewModel()
        {
            if (DataManager.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_CD_TYFSYY))
            {
                DateVis = Visibility.Collapsed;
            }
            else
            {
                DateVis = Visibility.Visible;
            }
            _selectDateTime = DateTime.Now;
            UpdateDisplayOprList();
        }
        #endregion

        #region Properties
        public List<PatientLinkModel> GetPatientLink()
        {
            return PatientLinkService.Instance.SechudlePatientByTime(
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day),
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day).AddDays(1)); ;
        }

        public ObservableCollection<DrugListItemCtrl2> DisplayOprList
        {
            get
            {
                return _displayOprList;
            }

            set
            {
                _displayOprList = value;
                //RaisePropertyChanged("DisplayOprList");
            }
        }

        public DateTime SelectDateTime
        {
            get
            {
                return _selectDateTime;
            }

            set
            {
                _selectDateTime = value;
                RaisePropertyChanged("SelectDateTime");
                //UpdateDisplayOprList();
            }
        }

        public DateTime StartTime
        {
            get
            {
                return _startTime;
            }

            set
            {
                _startTime = value;
            }
        }

        public DateTime EndTime
        {
            get
            {
                return _endTime;
            }

            set
            {
                _endTime = value;
            }
        }

        public Visibility DateVis
        {
            get
            {
                return _dateVis;
            }

            set
            {
                _dateVis = value;
            }
        }




        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods


        public void UpdateDisplayOprList()
        {
            DisplayOprList.Clear();

            List<PatientLinkModel> list = PatientLinkService.Instance.GetFinishOperByTime(
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day),
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day).AddDays(1));
            var tempDic = list.GroupBy(O => O.Status.InRoomTime).OrderBy(O => O.Key);

            List<DrugStatisticModel> lac = new List<DrugStatisticModel>();
            int index = 1;
            for (int i = 0; i < tempDic.Count(); i++)
            {
                var group = tempDic.ElementAt(i).OrderBy(O => O.Status.InRoomTime);
                foreach (var item in group)
                {
                    DrugStatisticModel model = new DrugStatisticModel()
                    {
                        Index = index,
                        PatientLink = item
                    };
                    SetDefaultVaule(model);
                    model = DrugDetail(model);
                    index++;
                    lac.Add(model);
                }
            }

            int itemsCount = lac.Count / itemCount;
            int remaining = lac.Count % itemCount;
            if (itemsCount >= 1)
            {
                for (int i = 0; i <= itemsCount; i++)
                {
                    DrugListItemCtrl2 ctrl = new DrugListItemCtrl2();
                    if (i == itemsCount && remaining != 0)
                    {
                        ctrl.ItemsSource = lac.GetRange(i * itemCount, remaining);
                        for (int j = 0; j < (itemCount - remaining); j++)
                        {
                            DrugStatisticModel model = new DrugStatisticModel();
                            model.Index = lac.Count + j + 1;
                            ctrl.ItemsSource.Add(model);
                        }
                        DisplayOprList.Add(ctrl);
                        break;
                    }
                    ctrl.SetBinding(DrugListItemCtrl2.SelectDateTimeProperty, new Binding("SelectDateTime")
                    {
                        Source = this,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    });
                    ctrl.ItemsSource = lac.GetRange(i * itemCount, itemCount);
                    DisplayOprList.Add(ctrl);
                }
            }
            else
            {
                DrugListItemCtrl2 ctrl = new DrugListItemCtrl2();
                for (int i = lac.Count; i < itemCount; i++)
                {
                    DrugStatisticModel model = new DrugStatisticModel();
                    if (i < list.Count)
                    {
                        SetDefaultVaule(model);
                        model = DrugDetail(model);
                    }
                    model.Index = i + 1;
                    lac.Add(model);
                }
                ctrl.ItemsSource = lac;
                DisplayOprList.Add(ctrl);
            }
        }
        public override void Refresh()
        {
            UpdateDisplayOprList();
        }

        public void Refresh(DateTime startTime, DateTime endTime)
        {
            DisplayOprList.Clear();
            List<PatientLinkModel> list = PatientLinkService.Instance.GetFinishOperByTime(startTime,
                endTime.AddDays(1));
            var tempDic = list.GroupBy(O => O.Status.InRoomTime).OrderBy(O => O.Key);
            List<DrugStatisticModel> lac = new List<DrugStatisticModel>();
            int index = 1;
            for (int i = 0; i < tempDic.Count(); i++)
            {
                var group = tempDic.ElementAt(i).OrderBy(O => O.Status.InRoomTime);
                foreach (var item in group)
                {
                    DrugStatisticModel model = new DrugStatisticModel()
                    {
                        Index = index,
                        PatientLink = item
                    };
                    SetDefaultVaule(model);
                    model = DrugDetail(model);
                    index++;
                    lac.Add(model);
                }
            }

            int itemsCount = lac.Count / itemCount;
            int remaining = lac.Count % itemCount;
            if (itemsCount >= 1)
            {
                for (int i = 0; i <= itemsCount; i++)
                {
                    DrugListItemCtrl2 ctrl = new DrugListItemCtrl2();
                    if (i == itemsCount && remaining != 0)
                    {
                        ctrl.ItemsSource = lac.GetRange(i * itemCount, remaining);
                        for (int j = 0; j < (itemCount - remaining); j++)
                        {
                            DrugStatisticModel model = new DrugStatisticModel();
                            model.Index = lac.Count + j + 1;
                            ctrl.ItemsSource.Add(model);
                        }
                        DisplayOprList.Add(ctrl);
                        break;
                    }
                    ctrl.SetBinding(DrugListItemCtrl2.SelectDateTimeProperty, new Binding("SelectDateTime")
                    {
                        Source = this,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    });
                    ctrl.ItemsSource = lac.GetRange(i * itemCount, itemCount);
                    DisplayOprList.Add(ctrl);
                }
            }
            else
            {
                DrugListItemCtrl2 ctrl = new DrugListItemCtrl2();
                for (int i = lac.Count; i < itemCount; i++)
                {
                    DrugStatisticModel model = new DrugStatisticModel();
                    if (i < list.Count)
                    {
                        SetDefaultVaule(model);
                        model = DrugDetail(model);
                    }
                    model.Index = i + 1;
                    lac.Add(model);
                }
                ctrl.ItemsSource = lac;
                DisplayOprList.Add(ctrl);
            }
        }

        public DrugStatisticModel DrugDetail(DrugStatisticModel model)
        {
            double ftn = 0;
            double rftn = 0;
            double sftn = 0;
            double mhj = 0;
            List<PatientDrugsModel> pdrugList = PatientDrugsService.Instance.GetPatientDrugs8Time(model.PatientLink.Id.ToString());
            foreach (var item in pdrugList)
            {
                if (item.Drugs.FullName.Contains("瑞芬太尼"))
                {
                    if (item.TotalAmountUnit.Equals("μg") || item.TotalAmountUnit.Equals("ug"))
                    {
                        rftn += item.TotalAmount.ToDouble() / 1000;
                    }
                    else
                    {
                        rftn += item.TotalAmount.ToDouble();
                    }
                }
                else if (item.Drugs.FullName.Contains("麻黄碱"))
                {
                    foreach (var itemDrug in item.Drugs.Items)
                    {
                        mhj += itemDrug.Dose.ToDouble();
                    }
                }
                else if (item.Drugs.FullName.Contains("舒芬太尼"))
                {
                    foreach (var itemDrug in item.Drugs.Items)
                    {
                        sftn += itemDrug.Dose.ToDouble();
                    }
                }
                else if (item.Drugs.FullName.Contains("芬太尼"))
                {
                    foreach (var itemDrug in item.Drugs.Items)
                    {
                        ftn += itemDrug.Dose.ToDouble();
                    }
                }
            }
            if (DataManager.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_CQ_FYBJY))
            {
                sftn += GetShuFenTaiNi(model.PatientLink);
                ftn += GetFenTaiNi(model.PatientLink);
            }
            if (ftn > 0)
            {
                model.DrugFTNDose = ftn.ToString();
            }
            if (rftn > 0)
            {
                model.DrugRFDose = rftn.ToString();
            }
            if (sftn > 0)
            {
                model.DrugSFDose = sftn.ToString();
            }
            if (mhj > 0)
            {
                model.DrugMHJDose = mhj.ToString();
            }
            return model;
        }
        public void SetDefaultVaule(DrugStatisticModel model)
        {
            model.DrugSFDose = "/";
            model.DrugSFNumber = "/";
            model.DrugSFRemain = "/";
            model.DrugRFDose = "/";
            model.DrugRFNumber = "/";
            model.DrugRFRemain = "/";
            model.DrugFTNDose = "/";
            model.DrugFTNNumber = "/";
            model.DrugFTNRemain = "/";
            model.DrugMHJDose = "/";
            model.DrugMHJNumber = "/";
            model.DrugMHJRemain = "/";
        }

        public double GetShuFenTaiNi(PatientLinkModel p)
        {
            double dose = 0;
            List<string> sftns = new List<string>();
            sftns = DataManager.Instance.GetXMLTableValue(p, "ShouShuZhengTong", new string[] { "gyss", "gyssftn" });
            if (sftns.Count == 2)
            {
                foreach (var item in sftns)
                {
                    dose += item.ToDouble();
                }
            }
            return dose;
        }

        public double GetFenTaiNi(PatientLinkModel p)
        {
            double dose = 0;
            List<string> ftns = new List<string>();
            ftns = DataManager.Instance.GetXMLTableValue(p, "ShouShuZhengTong", new string[] { "gyxf", "gysftn" });
            if (ftns.Count == 2)
            {
                foreach (var item in ftns)
                {
                    dose += item.ToDouble();
                }
            }
            return dose;
        }
        #endregion

        #region EventHandlers

        #endregion
    }
}


